<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>woodwhales-music</title>
    <link rel="icon" type="image/x-icon" th:href="@{/favicon.ico}">
    <script type="text/javascript" th:src="@{/webjars/jquery/jquery.min.js}"></script>
    <script type="text/javascript" th:src="@{/webjars/layui/layui.js}"></script>
    <link rel="stylesheet" th:href="@{/webjars/layui/css/layui.css}"/>
    <link rel="stylesheet" th:href="@{/css/my-css.css}"/>
</head>
<body>
<div class="layui-main site-inline">
    <fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
        <legend>woodwhales-music-admin</legend>
    </fieldset>

    <blockquote class="layui-elem-quote">移风易俗，莫善于乐。</blockquote>

    <form class="layui-form layui-form-pane" action="">
        <div class="layui-form-item">
            <label class="layui-form-label">搜索</label>
            <div class="layui-input-inline">
                <input type="text" name="searchInfo" id="searchInfo" lay-verify="title" autocomplete="off" placeholder="音乐名称、作者、专辑"
                       class="layui-input">
            </div>
            <div class="layui-input-inline">
                <button type="submit" class="layui-btn layui-btn-sm" lay-submit="" lay-filter="searchBtn">搜索</button>
                <button type="reset" class="layui-btn layui-btn-primary layui-btn-sm">重置</button>
            </div>
        </div>
    </form>

    <table class="layui-hide" id="musicTable" lay-filter="musicTable"></table>

    <script type="text/html" id="toolbarBtn">
        <div class="layui-btn-container">
            <button class="layui-btn layui-btn-xs" lay-event="add">添加</button>
            <button class="layui-btn layui-btn-xs layui-bg-cyan" lay-event="export">导出</button>
        </div>
    </script>

    <script type="text/html" id="barBtn">
        <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
        <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
    </script>

    <div th:replace="~{admin/footer::footer}"></div>

</div>

<script th:inline="none">
    layui.use(['form', 'layer', 'table'], function () {
        var form = layui.form,
            table = layui.table,
            layer = layui.layer;

        //监听提交
        form.on('submit(searchBtn)', function (data) {
            table.reload('musicTable', {
                url: './pageMusic',
                where: {
                    'searchInfo' : $('#searchInfo').val()
                }
                ,page: {
                    curr: 1
                }
            });
            return false;
        });

        //头工具栏事件
        table.on('toolbar(musicTable)', function (obj) {
            switch (obj.event) {
                case 'add':
                    location.href = './add'
                    break;
                case 'export':
                    location.href = './export'
                    break;
            }
        });

        //监听行工具事件
        table.on('tool(musicTable)', function (obj) {
            var data = obj.data;

            if (obj.event === 'del') {
                var postObj = {'id': data.id};
                console.log('postObj', postObj)
                layer.confirm('真的删除行么', function (index) {
                    $.ajax({
                        url: './deleteMusic',
                        type: 'post',
                        dataType: 'json',
                        contentType: "application/json",
                        async: true,
                        cache: false,
                        data: JSON.stringify(postObj),
                        success: function (res) {
                            table.reload('musicTable', {
                                url: './pageMusic'
                                , where: {}
                            });
                            layer.close(index);
                            layer.msg(res.msg, {icon: 1});
                        },
                        error: function (res) {
                            layer.close(index);
                            layer.msg('网络异常', {icon: 5});
                        }
                    });
                });

            } else if (obj.event === 'edit') {
                location.href = './edit?id=' + data.id
            }
        });

        table.render({
            elem: '#musicTable'
            , url: './pageMusic'
            , toolbar: '#toolbarBtn'
            , limit: 50
            , cols: [[
                {field: 'title', title: '音乐名称', templet: function (res) {
                    if(res.linked) {
                        return '<span style="color: #5FB878">'+ res.title +'</span>';
                    } else {
                        return '<span style="color: #FF5722">'+ res.title +'</span>';
                    }
                }},
                {field: 'artist', title: '作者'},
                {field: 'album', title: '专辑'},
                {field: 'sort', width: 60, title: '排序'},
                {field: 'gmtCreated', title: '创建时间'},
                {field: 'gmtModified', title: '更新时间'},
                {title: '操作', toolbar: '#barBtn'}
            ]]
            ,page: true
        });
    });
</script>
</body>
</html>